import '../models/fishing_report.dart';

class FishingReportService {
  // 模拟数据 - 钓况报告列表
  static List<FishingReport> _mockReports = [
    FishingReport(
      id: '1',
      title: '东湖钓场大丰收！',
      location: '湖北省武汉市',
      fishingSpotName: '东湖钓场',
      reportDate: DateTime.now().subtract(const Duration(hours: 2)),
      reporterName: '钓鱼达人小李',
      reporterAvatar: 'https://via.placeholder.com/50',
      fishTypes: ['鲫鱼', '草鱼', '鲤鱼'],
      fishCount: 15,
      totalWeight: 8.5,
      weatherCondition: '晴朗',
      temperature: 22.0,
      windDirection: '东南风',
      windSpeed: 2,
      waterCondition: '清澈',
      waterTemperature: 18.0,
      baitUsed: '蚯蚓、玉米',
      fishingTechnique: '台钓',
      timeRange: '06:00-12:00',
      images: [
        'https://via.placeholder.com/300x200',
        'https://via.placeholder.com/300x200',
      ],
      content: '今天天气不错，一大早就来到东湖钓场。水质很清澈，鱼儿活跃度很高。使用台钓技法，主要用蚯蚓和玉米做饵料。上午收获颇丰，钓到了15条鱼，总重量8.5公斤。其中最大的一条草鱼有2公斤重！',
      likeCount: 128,
      commentCount: 23,
      isLiked: false,
      rating: 4.8,
      difficulty: '中等',
      tips: ['早晨6-8点鱼儿最活跃', '使用新鲜蚯蚓效果更好', '保持安静避免惊扰鱼群'],
    ),
    FishingReport(
      id: '2',
      title: '西湖夜钓体验',
      location: '浙江省杭州市',
      fishingSpotName: '西湖钓场',
      reportDate: DateTime.now().subtract(const Duration(days: 1)),
      reporterName: '夜钓爱好者',
      reporterAvatar: 'https://via.placeholder.com/50',
      fishTypes: ['鲫鱼', '鲢鱼'],
      fishCount: 8,
      totalWeight: 4.2,
      weatherCondition: '多云',
      temperature: 18.0,
      windDirection: '无风',
      windSpeed: 0,
      waterCondition: '微浑',
      waterTemperature: 16.0,
      baitUsed: '红虫、面包虫',
      fishingTechnique: '夜钓',
      timeRange: '19:00-23:00',
      images: [
        'https://via.placeholder.com/300x200',
      ],
      content: '昨晚尝试了夜钓，虽然鱼获不如白天多，但体验很棒。夜晚的西湖很安静，只有偶尔的鱼儿跳水声。使用红虫做饵料，效果不错。',
      likeCount: 89,
      commentCount: 15,
      isLiked: true,
      rating: 4.2,
      difficulty: '困难',
      tips: ['夜钓需要准备充足的照明设备', '红虫在夜间效果更佳', '注意安全，最好结伴而行'],
    ),
    FishingReport(
      id: '3',
      title: '南湖野钓初体验',
      location: '湖南省长沙市',
      fishingSpotName: '南湖野钓点',
      reportDate: DateTime.now().subtract(const Duration(days: 2)),
      reporterName: '新手钓友',
      reporterAvatar: 'https://via.placeholder.com/50',
      fishTypes: ['鲫鱼'],
      fishCount: 3,
      totalWeight: 1.2,
      weatherCondition: '小雨',
      temperature: 15.0,
      windDirection: '北风',
      windSpeed: 3,
      waterCondition: '浑浊',
      waterTemperature: 14.0,
      baitUsed: '商品饵',
      fishingTechnique: '手竿钓',
      timeRange: '08:00-16:00',
      images: [
        'https://via.placeholder.com/300x200',
      ],
      content: '作为新手第一次野钓，虽然收获不多，但学到了很多。雨天钓鱼确实有挑战性，不过也有意外的收获。',
      likeCount: 45,
      commentCount: 8,
      isLiked: false,
      rating: 3.5,
      difficulty: '简单',
      tips: ['新手建议选择天气好的时候', '商品饵使用要适量', '耐心是钓鱼最重要的品质'],
    ),
    FishingReport(
      id: '4',
      title: '北湖路亚钓获黑鱼',
      location: '江苏省南京市',
      fishingSpotName: '北湖路亚区',
      reportDate: DateTime.now().subtract(const Duration(days: 3)),
      reporterName: '路亚高手',
      reporterAvatar: 'https://via.placeholder.com/50',
      fishTypes: ['黑鱼', '鲈鱼'],
      fishCount: 5,
      totalWeight: 6.8,
      weatherCondition: '阴天',
      temperature: 20.0,
      windDirection: '西风',
      windSpeed: 1,
      waterCondition: '清澈',
      waterTemperature: 17.0,
      baitUsed: '软虫、亮片',
      fishingTechnique: '路亚',
      timeRange: '07:00-11:00',
      images: [
        'https://via.placeholder.com/300x200',
        'https://via.placeholder.com/300x200',
        'https://via.placeholder.com/300x200',
      ],
      content: '今天路亚收获不错！用软虫钓到了几条黑鱼，最大的一条有2.5公斤。路亚钓法需要不断变换手法，很考验技巧。',
      likeCount: 156,
      commentCount: 31,
      isLiked: true,
      rating: 4.9,
      difficulty: '困难',
      tips: ['路亚需要掌握多种手法', '软虫对黑鱼很有效', '要根据鱼情调整拟饵'],
    ),
    FishingReport(
      id: '5',
      title: '城市公园小池塘惊喜',
      location: '上海市浦东新区',
      fishingSpotName: '世纪公园钓点',
      reportDate: DateTime.now().subtract(const Duration(days: 5)),
      reporterName: '都市钓客',
      reporterAvatar: 'https://via.placeholder.com/50',
      fishTypes: ['鲫鱼', '鲤鱼'],
      fishCount: 12,
      totalWeight: 5.6,
      weatherCondition: '晴朗',
      temperature: 25.0,
      windDirection: '南风',
      windSpeed: 2,
      waterCondition: '清澈',
      waterTemperature: 20.0,
      baitUsed: '蚯蚓、面包',
      fishingTechnique: '台钓',
      timeRange: '14:00-18:00',
      images: [
        'https://via.placeholder.com/300x200',
      ],
      content: '没想到城市公园的小池塘也能有这么好的收获！下午时光很惬意，钓鱼的同时还能欣赏公园美景。',
      likeCount: 78,
      commentCount: 12,
      isLiked: false,
      rating: 4.3,
      difficulty: '简单',
      tips: ['城市钓点要注意环保', '下午时段鱼儿比较活跃', '面包屑也是不错的饵料'],
    ),
  ];

  // 获取钓况报告列表
  static Future<List<FishingReport>> getFishingReports({
    int page = 1,
    int pageSize = 10,
    String? location,
    String? fishType,
    String? difficulty,
  }) async {
    // 模拟网络延迟
    await Future.delayed(const Duration(milliseconds: 800));

    List<FishingReport> filteredReports = List.from(_mockReports);

    // 按条件过滤
    if (location != null && location.isNotEmpty) {
      filteredReports = filteredReports
          .where((report) => report.location.contains(location))
          .toList();
    }

    if (fishType != null && fishType.isNotEmpty) {
      filteredReports = filteredReports
          .where((report) => report.fishTypes.contains(fishType))
          .toList();
    }

    if (difficulty != null && difficulty.isNotEmpty) {
      filteredReports = filteredReports
          .where((report) => report.difficulty == difficulty)
          .toList();
    }

    // 按时间排序（最新的在前）
    filteredReports.sort((a, b) => b.reportDate.compareTo(a.reportDate));

    // 分页
    int startIndex = (page - 1) * pageSize;
    int endIndex = startIndex + pageSize;
    if (startIndex >= filteredReports.length) {
      return [];
    }
    if (endIndex > filteredReports.length) {
      endIndex = filteredReports.length;
    }

    return filteredReports.sublist(startIndex, endIndex);
  }

  // 获取钓况统计数据
  static Future<FishingStats> getFishingStats() async {
    // 模拟网络延迟
    await Future.delayed(const Duration(milliseconds: 500));

    return FishingStats(
      totalReports: _mockReports.length,
      todayReports: _mockReports
          .where((report) =>
              report.reportDate.isAfter(DateTime.now().subtract(const Duration(days: 1))))
          .length,
      averageRating: _mockReports.isNotEmpty
          ? _mockReports.map((r) => r.rating).reduce((a, b) => a + b) / _mockReports.length
          : 0.0,
      mostPopularSpot: '东湖钓场',
      mostCaughtFish: '鲫鱼',
      fishTypeStats: {
        '鲫鱼': 4,
        '草鱼': 1,
        '鲤鱼': 2,
        '黑鱼': 1,
        '鲈鱼': 1,
        '鲢鱼': 1,
      },
      spotStats: {
        '东湖钓场': 1,
        '西湖钓场': 1,
        '南湖野钓点': 1,
        '北湖路亚区': 1,
        '世纪公园钓点': 1,
      },
      weeklyRatings: [4.2, 4.5, 4.1, 4.8, 4.3, 4.6, 4.4],
    );
  }

  // 获取单个钓况报告详情
  static Future<FishingReport?> getFishingReportById(String id) async {
    await Future.delayed(const Duration(milliseconds: 300));
    
    try {
      return _mockReports.firstWhere((report) => report.id == id);
    } catch (e) {
      return null;
    }
  }

  // 点赞/取消点赞
  static Future<bool> toggleLike(String reportId) async {
    await Future.delayed(const Duration(milliseconds: 200));
    
    final index = _mockReports.indexWhere((report) => report.id == reportId);
    if (index != -1) {
      final report = _mockReports[index];
      final newLikeCount = report.isLiked ? report.likeCount - 1 : report.likeCount + 1;
      
      _mockReports[index] = FishingReport(
        id: report.id,
        title: report.title,
        location: report.location,
        fishingSpotName: report.fishingSpotName,
        reportDate: report.reportDate,
        reporterName: report.reporterName,
        reporterAvatar: report.reporterAvatar,
        fishTypes: report.fishTypes,
        fishCount: report.fishCount,
        totalWeight: report.totalWeight,
        weatherCondition: report.weatherCondition,
        temperature: report.temperature,
        windDirection: report.windDirection,
        windSpeed: report.windSpeed,
        waterCondition: report.waterCondition,
        waterTemperature: report.waterTemperature,
        baitUsed: report.baitUsed,
        fishingTechnique: report.fishingTechnique,
        timeRange: report.timeRange,
        images: report.images,
        content: report.content,
        likeCount: newLikeCount,
        commentCount: report.commentCount,
        isLiked: !report.isLiked,
        rating: report.rating,
        difficulty: report.difficulty,
        tips: report.tips,
      );
      return true;
    }
    return false;
  }

  // 获取单个钓况报告详情
  static Future<FishingReport?> getReportDetail(String reportId) async {
    await Future.delayed(const Duration(milliseconds: 500)); // 模拟网络延迟
    
    try {
      final report = _mockReports.firstWhere((r) => r.id == reportId);
      return report;
    } catch (e) {
      return null;
    }
  }
}